package com.sunning.flutter_oupay;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FlutterOupayPlugin */
public class FlutterOupayPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Registrar registrar;
  private PermissionManager permissionManager;
  private OupayUnionPay oupayUnionPay;
//  private final OupayAlipay oupayAlipay;
//  private final OupayWechat oupayWechat;
//  private final OupayCMBPay oupayCmbPay;
  private OupayCCBPay oupayCcbPay;

//  public FlutterOupayPlugin(Registrar registrar){
//    this.registrar = registrar;
//    this.permissionManager = new PermissionManager(registrar.activity());
//    registrar.addRequestPermissionsResultListener(permissionManager);
////    // 注册ActivityResult回调
////    oupayUnionPay = new OupayUnionPay(registrar.activity());
////    registrar.addActivityResultListener(oupayUnionPay);
////
//////    oupayAlipay = new OupayAlipay(registrar.activity());
//////    oupayWechat = new OupayWechat(registrar.activeContext());
//////
//////    oupayCmbPay = new OupayCMBPay(registrar.activity());
//////    registrar.addActivityResultListener(oupayCmbPay);
//  }

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_oupay");
    channel.setMethodCallHandler(this);
  }




  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

//  public static void registerWith(Registrar registrar) {
//    final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_oupay");
//    channel.setMethodCallHandler(new FlutterOupayPlugin(registrar));
//  }

  @Override
  public void onMethodCall(MethodCall call, Result result) {
    //检验权限
    if( !permissionManager.requestPermission() ){
      result.error("oupay","check permission fail",null);
      return;
    }

    if (call.method.equals("checkInstallApps")) {
      String unpayAppid = call.argument("unpayAppid");
      String alipayAppid = call.argument("alipayAppid");
      String wechatAppid = call.argument("wechatAppid");
      String cmbAppid = call.argument("cmbAppid");
      String ccbAppid = call.argument("ccbAppid");
      Map<String, Boolean> resultMap = new HashMap<String,Boolean>();
      resultMap.put("unpayApp",oupayUnionPay.checkInstallApp(unpayAppid));
//      resultMap.put("alipayApp",oupayAlipay.checkInstallApp(alipayAppid));
//      resultMap.put("wechatApp",oupayWechat.checkInstallApp(wechatAppid));
//      resultMap.put("cmbApp",oupayCmbPay.checkInstallApp(cmbAppid));
      resultMap.put("ccbApp",oupayCcbPay.checkInstallApp(ccbAppid));
      result.success(resultMap);
    }else if (call.method.equals("unionPay")) {
      String payInfo = call.argument("payInfo");
      boolean isSandbox = call.argument("isSandbox");
      String urlScheme = call.argument("urlScheme");

      oupayUnionPay.startPay(payInfo,isSandbox,result);
    }else if (call.method.equals("aliPay")) {
//      String payInfo = call.argument("payInfo");
//      boolean isSandbox = call.argument("isSandbox");
//      String urlScheme = call.argument("urlScheme");
//
//      oupayAlipay.startPay(payInfo,isSandbox,result);

    }else if (call.method.equals("wechatPay")) {
//      String payInfo = call.argument("payInfo");
//      String appid = call.argument("appid");
//      String urlScheme = call.argument("urlScheme");
//
//      oupayWechat.starPay(appid,payInfo,result);
    }else if (call.method.equals("cmbchinaPay")) {
//      String payInfo = call.argument("payInfo");
//      String appid = call.argument("appid");
//      boolean isSandbox = call.argument("isSandbox");
//      String urlScheme = call.argument("urlScheme");
//
//      oupayCmbPay.starPay(appid,payInfo,isSandbox,result);
    }else if (call.method.equals("ccbnetPay")) {
      String payInfo = call.argument("payInfo");
//      boolean isSandbox = call.argument("isSandbox");
//      String urlScheme = call.argument("urlScheme");

      oupayCcbPay.startPay(payInfo,false,result);
    }else {
      result.notImplemented();
    }
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    permissionManager = new PermissionManager(binding.getActivity());
    binding.addRequestPermissionsResultListener(permissionManager);
    //云闪付
    oupayUnionPay = new OupayUnionPay(binding.getActivity());
    binding.addActivityResultListener(oupayUnionPay);
    //建行
    oupayCcbPay = new OupayCCBPay(binding.getActivity());
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {

  }
}
